package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.Log4Test;

/**
 * Created by WHOover on 13.07.2014.
 */
public class HomePage extends BasePage {
    private static final By searchLocator = By.className("header-search-input-text");
    private static final By foundLocator = By.className("search-result-goods");
    private static final By searchResultLocator = By.className("search-result");
    protected final String productFoundLocator = "//a[contains(text(),'%s')]";
    private static final By ticketsCategoryLocator = By.id("fatmenu_14");
    private static final By flightDirectionLocator = By.xpath("//*[contains(@href,'IEV/AMS')]");

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public boolean isOpened() {
        return driver.getCurrentUrl().equals(URL);
    }

    public boolean findItem(String item) {
        WebDriverWait wait = new WebDriverWait(driver, 25);

        Log4Test.info("searching product" + item);
        driver.findElement(searchLocator).clear();
        driver.findElement(searchLocator).sendKeys(item);
        driver.findElement(searchLocator).sendKeys(Keys.RETURN);
        wait.until(ExpectedConditions.presenceOfElementLocated(searchResultLocator));

        return driver.findElement(By.xpath(String.format(productFoundLocator,item))).isDisplayed();
    }

    public void openAirTicketsPage() {
        Log4Test.info("Open air tickets page.");
        Actions selectTicketsCategory = new Actions(driver);

        selectTicketsCategory.moveToElement(driver.findElement(ticketsCategoryLocator)).perform();
        wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(flightDirectionLocator))).click();
    }
}
